﻿using System;
using System.Globalization;


namespace advadev.CanOpen
{

    /// <summary>
    /// Represents a can open abort code
    /// </summary>
    public class CanOpenAbortCode
    {

        /// <summary>
        /// Initializes a new instance of the CanOpenAportCode class by the
        /// given parameter
        /// </summary>
        /// <param name="byte4">
        /// Byte 4
        /// </param>
        /// <param name="byte5">
        /// Byte 5
        /// </param>
        /// <param name="byte6">
        /// Byte 6
        /// </param>
        /// <param name="byte7">
        /// Byte 7
        /// </param>
        public CanOpenAbortCode(byte byte4, byte byte5, byte byte6, byte byte7)
        {
            Byte4 = byte4;
            Byte5 = byte5;
            Byte6 = byte6;
            Byte7 = byte7;
            HexString = "0x" + string.Format(CultureInfo.InvariantCulture, "{0:X2}", Byte7) + string.Format(CultureInfo.InvariantCulture, "{0:X2}", Byte6) + string.Format(CultureInfo.InvariantCulture, "{0:X2}", Byte5) + string.Format(CultureInfo.InvariantCulture, "{0:X2}", Byte4);
        }


        /// <summary>
        /// Gets the byte 4
        /// </summary>
        public byte Byte4
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the byte 5
        /// </summary>
        public byte Byte5
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the byte 6
        /// </summary>
        public byte Byte6
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the byte 7
        /// </summary>
        public byte Byte7
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the abort code as hex string
        /// </summary>
        public string HexString
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets a localized description for the abort code
        /// </summary>
        public string LocalizedDescription
        {
            get
            {
                try
                {
                    string localizedDescription = Properties.Resources.ResourceManager.GetString("CanOpenAbortCode_" + HexString.Substring(2));
                    if (!string.IsNullOrEmpty(localizedDescription))
                    {
                        return localizedDescription;
                    }
                }
                catch { }
                return HexString;
            }
        }

    }

}
